package vn.com.shoppie.util;

import java.util.Comparator;

import vn.com.shoppie.activity.MainActivity;
import vn.com.shoppie.sobject.ShoppieObject;
import vn.com.shoppie.sobject.Store;
import android.location.Location;
import android.util.Log;

import com.google.android.maps.GeoPoint;

public class StoreComparator implements Comparator<ShoppieObject> {
	@Override
	public int compare(ShoppieObject arg0, ShoppieObject arg1) {
		Store other1 = null;
		Store other2 = null;
		if (arg0 instanceof Store) {
			other1 = (Store) arg0;
		}
		if (arg1 instanceof Store) {
			other2 = (Store) arg1;
		}
		if (MainActivity.myLocation.getLatitudeE6() == 0
				|| MainActivity.myLocation.getLongitudeE6() == 0){
			Log.e("can not retreive current location", "OK");
			return 0;
		}
		float d1 = getDistanceInMets(MainActivity.myLocation,
				other1.getLocation());
		other1.distanceInM=d1+"";
		float d2 = getDistanceInMets(MainActivity.myLocation,
				other2.getLocation());
		other2.distanceInM=d2+"";
		if(d1-d2 > 0) return 1;
		else return -1;
	}
	public float getDistanceInMets(GeoPoint p1, GeoPoint p2) {
	    double lat1 = ((double)p1.getLatitudeE6()) / 1e6;
	    double lng1 = ((double)p1.getLongitudeE6()) / 1e6;
	    double lat2 = ((double)p2.getLatitudeE6()) / 1e6;
	    double lng2 = ((double)p2.getLongitudeE6()) / 1e6;
	    float [] dist = new float[1];
	    Location.distanceBetween(lat1, lng1, lat2, lng2, dist);
	    
	    return dist[0] /* 0.000621371192f*/;
	}
}